﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.GateUI {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import Events.*;
    import flash.utils.*;
    import org.aswing.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import flash.filters.*;
    import Display.*;
    import flash.text.*;
    import Display.aswingui.*;
    import L10n.*;

    public class KeyCell extends ItemCell {

        public static const CELL_WIDTH:int = 150;
        public static const BUTTON_FONT:ASFont = ASwingHelper.getBoldFont(12);
        public static const TEXT_FONT:ASFont = ASwingHelper.getBoldFont(16);
        public static const TEXT_COLOR:uint = 9463372;

        protected var m_amountNeeded:int;
        protected var m_itemCount:JLabel;
        private var m_isSendGift:Boolean = false;
        protected var m_buyCallback:Function = null;
        private var m_askCallback:Function;
        protected var m_buyHolder:Component;
        protected var m_askHolder:Component;
        protected var m_subButtonPanel:Component;
        protected var m_wishlistButton:AssetPane;
        protected var m_footerComponent:Component;
        protected var m_url:String;
        protected var m_name:String;
        protected var m_cost:Number;
        protected var m_count:Number;
        protected var m_displayObjectClass:Class;
        protected var m_value:Object;

        public function KeyCell(_arg1:String, _arg2:String, _arg3:Number, _arg4:Number, _arg5:Class=null){
            super(new SoftBoxLayout(SoftBoxLayout.Y_AXIS, -10, SoftBoxLayout.LEFT));
            this.m_url = _arg1;
            this.m_name = _arg2;
            this.m_cost = _arg3;
            this.m_count = _arg4;
            this.m_displayObjectClass = _arg5;
            m_initialized = false;
            Global.stage.addEventListener(DataItemEvent.WISHLIST_CHANGED, this.onWishlistChanged, false, 1, true);
        }
        override public function setCellValue(_arg1):void{
            if (_arg1.item){
                m_item = _arg1.item;
            };
            this.m_value = _arg1;
            _arg1 = (_arg1 as Object);
            this.m_amountNeeded = _arg1.amountNeeded;
            this.m_buyCallback = _arg1.buyCallback;
            this.m_askCallback = _arg1.askCallback;
            if (this.m_amountNeeded == 0){
                this.m_isSendGift = true;
            };
            if (this.m_url){
                m_loader = LoadingManager.load(this.m_url, onIconLoad, LoadingManager.PRIORITY_HIGH, onIconFail);
            } else {
                m_itemIcon = new this.m_displayObjectClass();
                addEventListener(Event.ADDED_TO_STAGE, onIconLoad, false, 0, true);
            };
        }
        protected function createHeaderComponent():Component{
            var _local1:Container = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local2:TextFormat = new TextFormat();
            _local2.align = TextFormatAlign.CENTER;
            var _local3:Number = TextFieldUtil.getLocaleFontSize(15, 10, null);
            var _local4:JTextField = ASwingHelper.makeTextField("", EmbeddedArt.TITLE_FONT, _local3, EmbeddedArt.brownTextColor, 0, _local2);
            _local4.setText((this.m_name + " "));
            TextFieldUtil.formatSmallCaps(_local4.getTextField(), new TextFormat(EmbeddedArt.TITLE_FONT, (_local3 + 4)));
            _local1.append(_local4);
            return (_local1);
        }
        protected function createFooterComponent():Component{
            var _local1:Container = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
            var _local2:String = ZLoc.t("Dialogs", "KeyAmount", {
                current:this.m_count.toString(),
                required:this.m_amountNeeded
            });
            this.m_itemCount = new JLabel(_local2);
            LocaleHelper.localSetting.ASwingHelper_setEasyBorder(this.m_itemCount, 1, 33, 1);
            this.m_itemCount.setForeground(new ASColor(EmbeddedArt.brownTextColor, 1));
            this.m_itemCount.setFont(TEXT_FONT);
            this.m_itemCount.setTextFilters([new GlowFilter(0xFFFFFF, 1, 2, 2, 10)]);
            this.m_itemCount.setPreferredWidth((CELL_WIDTH - 14));
            _local1.append(this.m_itemCount);
            var _local3:JPanel = ASwingHelper.makeFlowJPanel(FlowLayout.RIGHT);
            var _local4:DisplayObject = (new (GateDialog.assets["buildables_wishlistIcon"])() as DisplayObject);
            this.m_wishlistButton = new AssetPane(_local4);
            var _local5:int = this.m_wishlistButton.getPreferredWidth();
            _local3.append(this.m_wishlistButton);
            _local1.setPreferredHeight(this.m_wishlistButton.getPreferredHeight());
            if (!this.allowWishlistButton()){
                this.m_wishlistButton.visible = false;
            } else {
                this.m_wishlistButton.addEventListener(MouseEvent.CLICK, this.onWishlistClicked, false, 0, true);
                _local1.append(ASwingHelper.horizontalStrut(-(_local5)));
            };
            _local1.append(_local3);
            return (_local1);
        }
        protected function createImageComponent():Component{
            var _local1:Container = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            var _local3:DisplayObject = (new (GateDialog.assets["buildables_item"])() as DisplayObject);
            ASwingHelper.setSizedBackground(_local2, _local3, new Insets(5));
            var _local4:AssetPane = new AssetPane(m_itemIcon);
            ASwingHelper.setEasyBorder(_local4, 10, 10);
            _local2.appendAll(_local4);
            _local1.append(_local2);
            return (_local1);
        }
        protected function createTopButtonComponent():Component{
            var _local1:Container = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local2:CustomButton = new CustomButton(ZLoc.t("Dialogs", "AskButtonLabel"), null, "AskForKeyButtonUI");
            var _local3:Number = TextFieldUtil.getLocaleFontSize(18, 10, null);
            _local2.setFont(ASwingHelper.makeFont(EmbeddedArt.TITLE_FONT, _local3));
            _local2.addEventListener(MouseEvent.CLICK, this.onAskClick);
            _local1.append(_local2);
            return (_local1);
        }
        protected function createBottomButtonComponent():Component{
            var _local1:Container = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local2:AssetIcon = new AssetIcon((new EmbeddedArt.icon_cash() as DisplayObject));
            var _local3:CustomButton = new CustomButton(ZLoc.t("Dialogs", "BuyButtonLabel", {amount:this.m_cost.toString()}), _local2, "BuyGateKeyButtonUI");
            var _local4:Number = TextFieldUtil.getLocaleFontSize(18, 10, [{
                locale:"es",
                size:10
            }]);
            _local3.setFont(ASwingHelper.makeFont(EmbeddedArt.TITLE_FONT, _local4));
            _local3.addEventListener(MouseEvent.CLICK, this.onBuyClick);
            _local1.append(_local3);
            return (_local1);
        }
        protected function createActionComponent():Component{
            var _local1:Component;
            if (this.showButtons()){
                _local1 = this.createActionIncompleteComponent();
            } else {
                _local1 = this.createActionCompleteComponent();
            };
            return (_local1);
        }
        protected function createActionIncompleteComponent():Component{
            var _local1:Container = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            if (this.m_footerComponent){
                _local1.append(this.m_footerComponent);
            };
            if (this.m_askHolder){
                _local1.append(this.m_askHolder);
            };
            _local1.append(ASwingHelper.verticalStrut(3));
            if (this.m_buyHolder){
                _local1.append(this.m_buyHolder);
            };
            return (_local1);
        }
        protected function createActionCompleteComponent():Component{
            var _local1:Container = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local2:DisplayObject = (new (GateDialog.assets["buildables_check"])() as DisplayObject);
            var _local3:AssetPane = new AssetPane(_local2);
            _local1.append(_local3);
            return (_local1);
        }
        override protected function initializeCell():void{
            this.removeAll();
            if (this.m_isSendGift){
                this.buildSendGiftCell();
                return;
            };
            var _local1:Component = this.createHeaderComponent();
            var _local2:Component = this.createImageComponent();
            this.m_footerComponent = this.createFooterComponent();
            this.m_askHolder = this.createTopButtonComponent();
            this.m_buyHolder = this.createBottomButtonComponent();
            this.m_subButtonPanel = this.createActionComponent();
            this.append(_local1);
            this.append(_local2);
            this.append(ASwingHelper.verticalStrut(20));
            this.append(this.m_subButtonPanel);
            this.setPreferredWidth(CELL_WIDTH);
            ASwingHelper.prepare(this);
        }
        protected function showButtons():Boolean{
            var _local1:int = this.m_count;
            return (((_local1)>=this.m_amountNeeded) ? false : true);
        }
        protected function onBuyClick(_arg1:MouseEvent):void{
            var _local3:DisplayObject;
            var _local4:AssetPane;
            var _local5:JPanel;
            if (!LocaleHelper.localSetting.sns.cash_enable){
                return;
            };
            var _local2:Boolean = this.m_buyCallback(m_item);
            if (_local2){
                this.m_count++;
                this.m_itemCount.setText(ZLoc.t("Dialogs", "KeyAmount", {
                    current:this.m_count.toString(),
                    required:this.m_amountNeeded
                }));
                if (!this.showButtons()){
                    this.remove(this.m_subButtonPanel);
                    _local3 = (new (GateDialog.assets["buildables_check"])() as DisplayObject);
                    _local4 = new AssetPane(_local3);
                    _local5 = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
                    _local5.append(_local4);
                    this.append(_local5);
                    ASwingHelper.prepare(this);
                };
            };
        }
        private function onAskClick(_arg1:MouseEvent):void{
            this.m_askCallback(m_item, m_itemIcon);
        }
        private function buildSendGiftCell():void{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local2:Number = TextFieldUtil.getLocaleFontSize(16, 8, [{
                locale:"es",
                size:10
            }]);
            var _local3:JTextField = ASwingHelper.makeTextField(ZLoc.t("Dialogs", "SendGiftsToFriends"), EmbeddedArt.TITLE_FONT, _local2, EmbeddedArt.brownTextColor);
            TextFieldUtil.formatSmallCaps(_local3.getTextField(), new TextFormat(EmbeddedArt.TITLE_FONT, (_local2 + 4)));
            _local1.append(_local3);
            var _local4:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP, 0);
            var _local5:DisplayObject = (new (GateDialog.assets["buildables_item"])() as DisplayObject);
            ASwingHelper.setSizedBackground(_local4, _local5, new Insets(5));
            var _local6:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local7:AssetPane = new AssetPane(m_itemIcon);
            ASwingHelper.setEasyBorder(_local7, 10, 10);
            _local4.append(_local7);
            _local6.append(_local4);
            var _local8:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local9:CustomButton = new CustomButton(ZLoc.t("Dialogs", "SendGifts"), null, "AskForKeyButtonUI");
            var _local10:Number = TextFieldUtil.getLocaleFontSize(18, 10, null);
            _local9.setFont(ASwingHelper.makeFont(EmbeddedArt.TITLE_FONT, _local10));
            _local9.addActionListener(this.onSendGift);
            _local8.append(_local9);
            this.append(_local1);
            this.append(_local6);
            this.append(ASwingHelper.verticalStrut(30));
            this.append(_local8);
            ASwingHelper.prepare(this);
        }
        private function onSendGift(_arg1:Event):void{
            FrameManager.navigateTo("gifts.php?ref=neighbor_ladder");
        }
        protected function allowWishlistButton():Boolean{
            return (!(Global.player.isItemOnWishlist(m_item.name)));
        }
        protected function onWishlistClicked(_arg1:Event):void{
            if (Global.player.canAddToWishlist(m_item.name)){
                GameTransactionManager.addTransaction(new TAddToWishlist(m_item.name));
                dispatchEvent(new DataItemEvent(DataItemEvent.WISHLIST_CHANGED, this.m_item, null, true));
                this.m_wishlistButton.visible = false;
            };
        }
        public function performUpdate():void{
            this.initializeCell();
        }
        protected function onWishlistChanged(_arg1:DataItemEvent):void{
            if (_arg1.item.name == m_item.name){
                this.performUpdate();
            };
        }

    }
}//package Display.GateUI 
